"""
Models for the Track application.
"""

from django.db import models
from django.core import validators

from django.contrib.auth.models import User

# The models of the "Track" application

class Itinerary(models.Model):
  """Itineraries that the vehicles may follow"""
  name          = models.CharField(max_length=200, unique=True)
  slug          = models.SlugField(
                    prepopulate_from=('name',), unique=True,
                    help_text='This field is used for urls, etc.')
  description   = models.TextField(blank=True)
  user          = models.ForeignKey(User)
  
  def __unicode__(self):
    return '%(name)s (%(user)s)'%{
      'name': self.name,
      'user': self.user}
  
  class Meta:
    verbose_name_plural = 'itineraries'
  
  class Admin:
    list_filter   = ('user',)
    search_fields = ('name', 'slug', 'description', 'user__username')

class Checkpoint(models.Model):
  """Each Itinerary has several Checkpoints, that define a polyline."""
  itinerary = models.ForeignKey(Itinerary)
  order     = models.IntegerField(
                core=True,
                help_text='This field indicates the order of checkpoints')
  x         = models.FloatField(core=True)
  y         = models.FloatField(core=True)
  
  def __unicode__(self):
    return '%(itinerary)s [%(order)d] (%(x)s, %(y)s)'%{
      'itinerary': self.itinerary.name,
      'order':     self.order,
      'x':         self.x,
      'y':         self.y}
  
  class Meta:
    ordering        = ['itinerary', 'order']
    unique_together = ('itinerary', 'order')

class Vehicle(models.Model):
  """Information about the moving vehicles. Each Vehicle has a position, and may be linked to an Itinerary."""
  name        = models.CharField(max_length=200, unique=True)
  slug        = models.SlugField(
                  prepopulate_from=('name',), unique=True,
                  help_text='This field is used for urls, etc.')
  description = models.TextField(blank=True)
  user        = models.ForeignKey(User)
  date        = models.DateTimeField('Last modification date', auto_now=True)
  x           = models.FloatField(validator_list=[validators.NumberIsInRange(0, 360)])
  
  maxLatitude = 85
  y           = models.FloatField(
                  validator_list=[validators.NumberIsInRange(-maxLatitude, maxLatitude)])
  
  checkpoint  = models.ForeignKey(Checkpoint, null=True, blank=True)
  advancement = models.FloatField(null=True, blank=True)
  
  def __unicode__(self):
    return '%(vehicle)s (%(x)s, %(y)s) - %(date)s%(position)s'%{
      'vehicle':  self.name,
      'x':        self.x,
      'y':        self.y,
      'date':     self.date,
      'position': u' at %s (adv %.2f)'%(
        self.checkpoint,
        self.advancement if self.advancement else 0.0) if self.checkpoint else ''}
  
  class Meta:
    pass
  
  class Admin:
    date_hierarchy = 'date'
    list_filter    = ('user',)
    search_fields  = ('name', 'slug', 'user__username', 'description')
  

class Position(models.Model):
  """Log of positions by Vehicle."""
  vehicle     = models.ForeignKey(Vehicle)
  date        = models.DateTimeField(auto_now_add=True)
  x           = models.FloatField()
  y           = models.FloatField()
  checkpoint  = models.ForeignKey(Checkpoint, null=True, blank=True)
  advancement = models.FloatField(null=True, blank=True)
  
  def __unicode__(self):
    return '%(vehicle)s (%(x)s, %(y)s) - %(date)s'%{
      'vehicle': self.vehicle.name, 'x': self.x, 'y': self.y, 'date': self.date}
  
  class Meta:
    unique_together = ('vehicle', 'date')
    get_latest_by   = 'date'
  
  class Admin:
    date_hierarchy = 'date'
    list_filter    = ('vehicle',)
    search_fields   = ('vehicle__name', 'vehicle__slug')
